const OrdercqServices = require("../services/ordercqServices")

class OrdercqControllers {
  constructor() {
    this.addNewOrdercq = this.addNewOrdercq.bind(this)
    this.delOrdercqById = this.delOrdercqById.bind(this)
    this.getOrdercqById = this.getOrdercqById.bind(this)
    this.getAllOrdercqs = this.getAllOrdercqs.bind(this)
  }

  // 统一响应函数
  handleRequest = async (serviceMethod, req, res, successStatus = 200) => {
    try {
      const result = await serviceMethod(req)
      if (result.code === 0) {
        res.status(successStatus).json(result) // 成功
      } else {
        res.status(result.statusCode || 400).json(result) // 失败
      }
    } catch (error) {
      res.status(500).json({
        code: 1,
        msg: '服务器内部错误',
      })
    }
  }

  async addNewOrdercq(req, res) {
    await this.handleRequest(
      () => OrdercqServices.addNewOrdercq(req.body),
      req,
      res
    )
  }

  async delOrdercqById(req, res) {
    await this.handleRequest(
      () => OrdercqServices.delOrdercqById(req.params.id),
      req,
      res
    )
  }

  async getOrdercqById(req, res) {
    await this.handleRequest(
      () => OrdercqServices.getOrdercqById(req.params.id),
      req,
      res
    )
  }

  async getAllOrdercqs(req, res) {
    await this.handleRequest(
      () => OrdercqServices.getAllOrdercqs(),
      req,
      res
    )
  }

}

module.exports = new OrdercqControllers